#include "CurseUtils.h"
#include <string.h>
#include <stdlib.h>


// ----------------------------------------------------------------
void curseEnalbeEcho() {
    echo();
}
void curseDisalbeEcho() {
    noecho();
}
void curseEnableCharBreak() {
    cbreak();
}
void curseDisbleCharBreak() {
    nocbreak();
}
void curseKeyboardUse(WINDOW *window) {
    keypad(window, true);
}
void curseKeyboardUnUse(WINDOW *window) {
    keypad(window, false);
}

// ---------------------------------------------------------------- Screen
void createCurseScreen() {
    initscr();
}
void curseScreenRefresh() {
    refresh();
}
void destroyCurseScreen() {
    endwin();
}

// ---------------------------------------------------------------- Window
void curseWindowBox(CurseWindow *window, int v, int h) {
    box(window, v, h);
}
void curseWindowRefresh(CurseWindow *window) {
    wrefresh(window);
}
void desctroyCurseWindow(CurseWindow *window) {
    // UNUSED(window);
}

CurseWindow* createCurseWindow(int height, int width, int y, int x) {
    return newwin(height, width, y, x);
}
CurseWindow* createCurseWindowBox(int height, int width, int y, int x) {
    CurseWindow* window = createCurseWindow(height, width, y, x);
    curseWindowBox(window, 0, 0);
    return window;
}
CurseWindow* createCurseWindowRect(int height, int width, int x, int y, int vc, int hc) {
    CurseWindow *window = createCurseWindow(height, width, x, y);
    curseWindowBox(window, vc, hc);
    return window;
}
CurseWindow* createCurseWindowRefresh(int height, int width, int y, int x) {
    CurseWindow* window = createCurseWindow(height, width, y, x);
    curseWindowRefresh(window);
    return window;
}
CurseWindow* createCurseWindowBoxRefresh(int height, int width, int x, int y) {
    CurseWindow* window = createCurseWindowBox(height, width, x, y);
    curseWindowRefresh(window);
    return window;
}
CurseWindow* createCurseWindowRectRefresh(int height, int width, int x, int y, int v, int h) {
    CurseWindow* window = createCurseWindowRect(height, width, x, y, v, h);
    curseWindowRefresh(window);
    return window;
}


// ---------------------------------------------------------------- Cursors
int curseWindowCursorMove(CurseWindow* window, int y, int x) {
    return wmove(window, y, x);
}
// ---------------------------------------------------------------- Border
// ---------------------------------------------------------------- Attributes
// ---------------------------------------------------------------- Colors
// ---------------------------------------------------------------- Terminal
void saveTerminalProgress() {
    def_prog_mode();
}
// ---------------------------------------------------------------- Output

#ifndef HAVE_VASPRINTF
int vasprintf(char **string, const char *format, va_list args) {
    *string = 0;

    va_list copy;
    va_copy(copy, args);
    int count = vsnprintf(NULL, 0, format, args);
    if (count >= 0) {
        char *buffer = (char*)malloc(count+1);
        if (buffer != NULL) {
            count = vsnprintf(buffer, count+1, format, copy);
            if (count < 0) {
                free(buffer);
            } else {
                *string = buffer;
            }
        }
    }
    va_end(copy);

    return count;
}
#endif  //!HAVE_VASPRINTF

// c_vasprintf()

static int c_strdup_vasprintf(char **val, const char *fmt, va_list args) {
    int count = vasprintf(val, fmt, args);
    if (count < 0) {
        *val = NULL;
        count = 0;
    }
    return count;
}

int curseWindowPrintf(CurseWindow *window, char *format, ...) {
    int ret = 0;
    va_list args;

    va_start(args, format);
    // 1. 自定义 vasprintf
    // char *s = NULL;
    // int slen = c_strdup_vasprintf(&s, format, args);
    // wprintw(window, s);
    // free(s);   
    
    // 2. use 内置 vw_printw
    ret = vw_printw(window, (const char *)format, args);
    va_end(args);
    return ret;
}
// ---------------------------------------------------------------- Input
// ---------------------------------------------------------------- Menu
// ---------------------------------------------------------------- KeyBoard
// ---------------------------------------------------------------- Other Opers
